package icegps.com.bhmap.drawhelp;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.SystemClock;

import java.util.List;

import icegps.com.bhmap.App;
import icegps.com.bhmap.bean.TargetPoint;
import icegps.com.bhmap.gen.TargetPointDao;
import icegps.com.bhmap.view.IMapView;

/**
 * Created by 111 on 2017/12/5.
 */

public class DrawRect {
    private Paint paint;
    private final TargetPointDao targetPointDao;
    private IMapView iMapView;
    private int colors[] = {
            0xFF4cff90, 0xFF4effb4, 0xFF53ffe0, 0xFF57ffff,
            0xFF5de4ff, 0xFF5dd0ff, 0xFF60bfff, 0xFF6cb2f8,
            0xFF68a9fe, 0xFF46ff70, 0xFF41fe4a, 0xFF5bff40,
            0xFF77ff3d, 0xFF94ff36, 0xFFa9ff2f, 0xFFc5fe2d,
            0xFFdbfe2a, 0xFFebfe29, 0xFFfffc22, 0xFFfced1f,
            0xFFf8dd18, 0xFFfdec15, 0xFFf7c413, 0xFFf7b511,
            0xFFf4af05, 0xFFe99b01, 0xFFe29100, 0xFFda8700,
            0xFFd77e00, 0xFFc97500, 0xFFc26a00, 0xFFb36100, 0xFFa75400};
    private List<TargetPoint> targetPoints;

    public DrawRect(IMapView iMapView) {
        this.iMapView = iMapView;
        paint = new Paint();
        paint.setAntiAlias(true);
        targetPointDao = App.getInstance().getDaoSession().getTargetPointDao();
        new LoadDataThread().start();
    }

    public void drawRect(Canvas canvas) throws InterruptedException {

        if (targetPoints == null) return;

        float rLength = iMapView.getRLength();

        float multiple = iMapView.getMultiple();

        for (int i = 0; i < targetPoints.size(); i++) {
            TargetPoint targetPoint = targetPoints.get(i);

            double x = targetPoint.getPointX();

            double y = targetPoint.getPointY();

            int height = (int) targetPoint.getHeight();

            paint.setColor(colors[height]);

            RectF rect = new RectF();

            rect.left = (float) (x * iMapView.getRLength() - rLength / 2) * multiple;

            rect.top = (float) (y * rLength - rLength / 2) * multiple;

            rect.right = (rect.left + rLength * multiple);

            rect.bottom = (rect.top + rLength * multiple);

            canvas.drawRect(rect, paint);

        }

    }

    class LoadDataThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (true) {
                targetPoints = targetPointDao.loadAll();
                SystemClock.sleep(200);
            }
        }
    }
}



